﻿using AutoMapper;
using MediatR;
using RBAC_Uthorization.Domain;
using RBAC_Uthorization.Infrstructre.Interface;
using RBAC_Uthorization.Read.API.Application.Command.OffIcCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Response.offlc;

namespace RBAC_Uthorization.Read.API.Application.CommandHandlers.OfflcHandler
{
    public class OfflcSelectShowCommandHandlers : IRequestHandler<OfflcSelectShowCommand, ApiResult<List<offlcSelectShowDto>>>
    {
        private readonly IOfficRepository _offic;
        private readonly IMapper _mapper;
        private readonly ILogger<OfflcSelectShowCommandHandlers> _logger;

        public OfflcSelectShowCommandHandlers(ILogger<OfflcSelectShowCommandHandlers> logger, IMapper mapper, IOfficRepository offic)
        {
            _logger = logger;
            _mapper = mapper;
            _offic = offic;
        }

        public async Task<ApiResult<List<offlcSelectShowDto>>> Handle(OfflcSelectShowCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.offlc_id != "10086")
                {
                    var officEntity = await _offic.GetAllAsunc(x=>x.id==request.offlc_id&&x.sys_id==request.sys_id);
                    var dto = _mapper.Map<List<offlcSelectShowDto>>(officEntity);
                    return new ApiResult<List<offlcSelectShowDto>>() { Data = dto };
                }
                else
                {
                    var officEntity = await _offic.GetAllAsync();
                    var dto = _mapper.Map<List<offlcSelectShowDto>>(officEntity);
                    return new ApiResult<List<offlcSelectShowDto>>() { Data = dto };
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理OfflcSelectShowCommand时发生错误");
                return new ApiResult<List<offlcSelectShowDto>>(ResultCode.Fail, "服务器内部错误,请稍后尝试");
            }
        }
    }
}
